﻿using System;
using System.Linq;
using System.Text;
using System.Web;
using BO;
using Channelvn.Cached.Common;
using EntityFramework;
using Rewrite;

namespace ProjectQuaDep
{
    public partial class ListProduct : System.Web.UI.Page
    {
        protected const int PageSize = 9;
        protected bool noItem = true;
        private const string ProductFocusItemFormat = @"<div class=""item mb30 {0}"">
                    {1}
                    <div class=""name"">
                        {2}</div>
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}"">
                            <img title=""{10}"" alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""code-name"">
                        {6}</div>
                    <div class=""desc"">
                        {7}</div>
                    <div class=""shopping"">
                        <div class=""price"">
                            <div class=""current"">
                                {8}</div>
                            <div class=""discount"">
                                {9}</div>
                        </div>
                        <a href=""javascript:void(0);"" class=""cart""></a>
                    </div>
                    <div class=""chitietshow"">
                        <a href=""{4}"" title=""{3}"">Chi tiết »</a>
                    </div>
                </div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";
        int iZoneid = 9999;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
            var catUrl = Lib.QueryString.CatUrl;
            var pageIndex = Lib.QueryString.PageIndex;
            var pageType = Lib.QueryString.PageType;
            var CompanyID = Lib.QueryString.CompanyID;
            var ZoneID = Lib.QueryString.ZoneID;
            if (string.IsNullOrEmpty(catUrl)) _301Redirection.RedirectTo("/404.aspx");
            ZoneProduct obj;
            if (pageType == 7) // List Product Company
            {
                obj = ZoneProductBO.GetZoneProductById(ZoneID);
                if (obj != null)
                {
                    #region nav
                    {
                        var sb = new StringBuilder();
                        //sb.AppendFormat("<a href='/c/{0}.htm' title='{1}' >{2}</a> > ", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);
                        sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);
                        var objChild = CompaniesBO.GetCompaniesByID(CompanyID);
                        if (objChild != null)
                            sb.AppendFormat("<a href='/c/{0}.htm' title='{1}' >{2}</a> ", ZoneID + "/" + CompanyID + "/" + HttpUtility.HtmlEncode(objChild.NameCp), HttpUtility.HtmlEncode(objChild.NameCp), objChild.NameCp);
                        ltrNav.Text = sb.ToString();
                    }
                    #endregion
                }
            }
            else
            {
                obj = ZoneProductBO.GetZoneProductByCatUrl(catUrl);
                if (obj != null)
                {
                    #region nav

                    {
                        var sb = new StringBuilder();
                        if (obj.ParentId != null && obj.ParentId > 0)
                        {
                            var objChild = ZoneProductBO.GetZoneProductById(obj.ParentId.Value);
                            if (objChild != null)
                                sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", objChild.url, HttpUtility.HtmlEncode(objChild.title), objChild.title);
                        }
                        sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a>", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);

                        ZoneProduct zd = ZoneProductBO.GetZoneProductDetailByUrl(catUrl);
                        ltrNav.Text = sb.ToString();
                    }
                    #endregion

                    
                }
            }

            #region 2 product top
            {
                //rptProductTopFocus.DataSource = ProductsBO.GetTopProductIsFocusHome(2, obj.ZoneId,CompanyID);
                //rptProductTopFocus.DataBind();
            }
            #endregion

            #region list product
            {  
                if (catUrl == "danh-sach-san-pham")
                {
                    //show all sp
                    iZoneid = 9999;
                }
                else
                {
                    //show sp theo dk
                    iZoneid = obj.ZoneId;
                }
                var lst = ProductsBO.GetPagingProductByZoneProduct(pageIndex, PageSize, iZoneid, CompanyID);
                if (lst.Any())
                {
                    noItem = false;
                    var sb = new StringBuilder();
                    var count = 1;
                    foreach (var item in lst)
                    {
                        var cls = count % 3 == 0 ? "mr0" : string.Empty;
                        long date = 0;
                        long.TryParse(item.EndDateNew, out date);
                        var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                        var price = item.TypeSale == 1 ? "Giá: Liên hệ" : Utils.ConvertMoneyNice(item.price.ToString(), BO.Const.FormatMoney);
                        var sale = item.TypeSale == 1 ? string.Empty : Utils.ConvertMoneyNice(item.sale.ToString(), BO.Const.FormatMoney);
                        var sapo = item.Sapo.ToString();
                        if (int.Parse(item.sale.ToString()) == 0)
                        {
                            sale = "";
                        }
                        sb.AppendFormat(ProductFocusItemFormat, cls
                            , pNew
                            , item.Title
                            , HttpUtility.HtmlEncode(item.Title)
                            , item.Url
                            , BO.Utils.CropImage(172, 140, BO.Utils.GetOneImagesByArr(item.Avatar))
                            , item.ProductCode
                            , BO.Utils.getSubStringNice(item.Sapo, 50)
                            , price
                            , sale
                            , sapo);
                        count++;
                    }
                    if (lst.Count > 0)
                    {
                        ltrListFocus.Text = sb.ToString();
                    }
                    else
                    {
                        ltrListFocus.Text = "<h1>Hiện chưa có sản phẩm nào ở danh mục này</h1>";
                    }
                }
            }
            #endregion

            #region paging
            this.ctrPagingProduct.PageSize = PageSize;
            this.ctrPagingProduct.SumPage = ProductsBO.GetPagingNumber(iZoneid, PageSize, CompanyID);

            this.ctrPagingProduct.Visible = !noItem ? true : false;

            this.ctrPagingProduct.LinkSite = "/" + Lib.QueryString.url + "/";
            this.ctrPagingProduct.Padding(pageIndex);
            #endregion
            }
            catch (Exception)
            {

                ltrNav.Text = "Hiện chưa có sản phẩm";
            }
        }
    }
}